package com.wenjiang.controller;


import cn.hutool.core.bean.BeanUtil;
import com.wenjiang.common.annotation.LimitFlow;
import com.wenjiang.common.annotation.TokenUserId;
import com.wenjiang.common.dto.SecKillGoodParams;
import com.wenjiang.common.dto.SecKillParams;
import com.wenjiang.entity.MallSecKillGood;
import com.wenjiang.service.MallSecKillGoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/mallSecKill")
public class MallSecKillGoodController {

    @Autowired
    MallSecKillGoodService mallSecKillGoodService;

    @RequestMapping("/add")
    public  Object addSecKill(@RequestBody SecKillParams params){
        //转换日期
        MallSecKillGood mallSecKillGood = new MallSecKillGood();
        BeanUtil.copyProperties(params, mallSecKillGood);

        return  mallSecKillGoodService.addSecKill(mallSecKillGood);
    }


    /**
     * 获取当前秒杀商品的信息
     */

    @RequestMapping("/getList")
    public  Object getSeckKillGood(){
        return  mallSecKillGoodService.getSecKillGood();
    }


    /**
     * 秒杀接口 (限流)
     */

    @RequestMapping("/SecKillGood")
    @LimitFlow(name = "test" ,token = 10 , timeout = 1)
    public  Object testSecKill(@RequestBody SecKillGoodParams secKillGoodParams, @TokenUserId Long userId){
        secKillGoodParams.setUserId(userId);
        return mallSecKillGoodService.SecKillGood(secKillGoodParams);
    }
}
